package DatabaseManaging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcCountriesDao implements ParametersDAO{
	private JdbcTemplate jdbcTemplate;

	public JdbcCountriesDao() {
	}
	
	@Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
	
	@Override
	public ArrayList<Object> fromMapToListElements(
			ArrayList<Map<String, Object>> resultQuery) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Object> findElementby(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertElement(Object ob) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateAbbsByName(String id, ArrayList<String> abbs) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Map<ArrayList<String>, String> getMapwithAllElements() {
		String sql = "SELECT * from akn_country_dim";
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
		HashMap<ArrayList<String>, String>  resultList = toMapElements(resultQuery);

		return resultList;
	}
	private HashMap<ArrayList<String>, String> toMapElements(
			ArrayList<Map<String, Object>> resultQuery) {
		HashMap<ArrayList<String>,String> resultMap=new HashMap<ArrayList<String>,String>();
		for(int i=0;i<resultQuery.size();i++){
        	Map<String, Object> mapElements=resultQuery.get(i);
        	Object id=mapElements.get("country_dim_id");
        	ArrayList<String> paramCountries=new ArrayList<String>();
        	paramCountries.add((String)mapElements.get("country_name"));
        	String abb=(String)mapElements.get("country_abbr");
        	if(!abb.isEmpty()){
        		paramCountries.add(abb);
        	}
        	resultMap.put(paramCountries,id.toString());
        }
		return resultMap;
	}
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("application-context-persist.xml");
		JdbcCountriesDao dao=(JdbcCountriesDao) context.getBean("JdbcCountriesDao");
		System.out.println(dao.getMapwithAllElements());
		
	}
}
